package com.manager.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.manager.bean.student;
import com.manager.service.StudentService;


@Controller
public class studentController {
	
	@Resource
	public StudentService studentService;
	
	@RequestMapping("/")
	public String index()
	{
		return "redirect:index";
	}
	
	@RequestMapping("/index")
	public String getindex(HttpSession session,HttpServletRequest request){
		List<student> stu=studentService.listStudent();
		session.setAttribute("student", stu);
		return "index";
	}
	
	@RequestMapping("search")
	public String search(HttpSession session,HttpServletRequest request,String sid){
		if(sid==""){
			return "redirect:index";
		}
		else{
			List<student> findstu=studentService.findStudent(Integer.parseInt(sid));
			session.setAttribute("student", findstu);
			return "index";
		}
	}
	
	@RequestMapping("delete")
	public String delete(HttpSession session,HttpServletRequest request,int id){
		studentService.deleteStudent(id);
		return "redirect:index";
	}
	
	@RequestMapping("addstudent")
	public String addstudent(HttpSession session,HttpServletRequest request,
			int addid,String addname,String addsex,int addgrade,int addtel){
		studentService.addStudent(new student(addid, addname, addsex, addgrade, addtel));
		return "redirect:index";
	}
	
	@RequestMapping("updatestudent")
	public String updatestudent(HttpSession session,HttpServletRequest request,
			int id,String name,String sex,int grade,int tel,int oldid){
		studentService.updateStudent(new student(id, name, sex, grade, tel,oldid));
		return "redirect:index";
	}
}